package com.example.administrator.newsapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.administrator.newsapp.pojo.JokeData;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * ClassName:
 * Description:
 */

public class JokeFragmentTab extends Fragment{
    private static final int POST_SUCCESS = 2001;
    private View mContentView;
    private Handler mHandler;
    private ListView mListView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(mContentView==null){
            mContentView=inflater.inflate(R.layout.joke_fragment_tab,container,false);
        }
        ViewGroup parent = (ViewGroup) mContentView.getParent();
        if(parent!=null){
            parent.removeView(mContentView);
        }
        return mContentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mListView=view.findViewById(R.id.list_view);
        final String url=getTag();
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                RequestBody requestBody = new FormBody.Builder()
                        .add("showapi_appid","66826")
                        .add("showapi_sign","5abab2381d014f32a952c4344d619dc0")
                        .add("page","1")
                        .add("maxResult","20").build();
                final Request request = new Request.Builder().url(url).post(requestBody).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("CALL", e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Message message = new Message();
                        message.what=POST_SUCCESS;
                        message.obj=response.body().string();
                        mHandler.sendMessage(message);
                    }
                });
            }
        }).start();

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what==POST_SUCCESS){
                    String result = (String)msg.obj;
                    analyzeJSONFromGSON(result);
                    Log.e("RESULT", result);
                    return true;
                }
                return false;
            }
        });
    }

    private void analyzeJSONFromGSON(String result) {
        Gson gson = new Gson();
        JokeData data = gson.fromJson(result,JokeData.class);
        JokeData.ShowapiResBodyBean showapiResBodyBean = data.getShowapi_res_body();
        List<JokeData.ShowapiResBodyBean.ContentlistBean> contentlist = showapiResBodyBean.getContentlist();
        JokeListAdapter adapter = new JokeListAdapter(mContentView.getContext(),contentlist);
        mListView.setAdapter(adapter);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
